package com.nas.crocus.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.nas.crocus.domain.Alojamiento;
import com.nas.crocus.domain.Hotel;
import com.nas.crocus.exception.EntidadNoUnicaException;
import com.nas.crocus.exception.NotFoundException;
import com.nas.crocus.service.HotelService;


@Controller
@RequestMapping(value = "/hotel")
public class HotelController {
    
    @Autowired
    private HotelService hotelService;
    
    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Collection<Hotel> buscarTodos() {
        Collection<Hotel> hoteles = hotelService.buscar();
        //Esta inicializado en forma lazy, problema con jackson
        ArrayList <Alojamiento> alojamientos = new ArrayList<Alojamiento>();
        for (Hotel hotel : hoteles) {
            hotel.setAlojamientos(alojamientos);
        }
        return hoteles;
    }

    @RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
    public @ResponseBody Hotel obtenerHotel(@PathVariable Long id) throws NotFoundException {
        Hotel hotel = hotelService.buscar(id);
        if (hotel == null) {
            throw new NotFoundException("hotel no encontrado");
        }
        return hotel;
    }
    
    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public @ResponseBody Hotel crear(@RequestBody Hotel hotel) throws EntidadNoUnicaException {
        hotelService.guardar(hotel);
        return hotel;
    }

    @RequestMapping(method = RequestMethod.PUT)
    public @ResponseBody Hotel actualizar(@RequestBody Hotel hotel) throws EntidadNoUnicaException {
        hotelService.actualizar(hotel);
        return hotel;
    }
    
    
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NotFoundException.class)
    public @ResponseBody Map<String, String> handleException(NotFoundException ex) {
        Map<String, String> fault = new HashMap<String, String>();
        fault.put("error", ex.getMensaje());
        return fault;
    }
    
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    @ExceptionHandler(EntidadNoUnicaException.class)
    public @ResponseBody Map<String, String> handleException(EntidadNoUnicaException ex) {
        Map<String, String> fault = new HashMap<String, String>();
        fault.put("error", ex.getMensaje());
        return fault;

    }
}
